<?php
namespace app\admin\controller;
use think\Controller;
use Util\Sysdb;
use Util\Tool;
use think\facade\Cache;

class BaseAdmin extends Controller
{
    public function __construct(){
        //用户session判断
        parent::__construct();
        if (!session('admin')){
            header('location:'.'//'.$_SERVER['HTTP_HOST'].'/admin/Account/login');
            exit;
        }
        $this->_admin = session('admin');// 使用助手函数session取值（当前作用域--浏览器）
        $this->assign('admin',$this->_admin);//模板变量赋值{$this->_admin}
        $this->Tool = new Tool;
        // 判断用户是否有权限
        $this->db = new Sysdb;//实例化封装数据库类
        $group = $this->db->table('admin_groups')->where(array('gid'=>$this->_admin['gid']))->item();
        if(!$group){
            $this->request_error('对不起，您没有权限');
        }
        if($group['gid']!=1){
            $rights = json_decode($group['rights']);//将存储的json权限数据转为数组
            // 当前访问菜单
            $controller = request()->controller();//用request接收当前页面控制器名称
            $action = request()->action();//用request接收当前页面操作名称
            $res = $this->db->table('admin_menus')->where(array('controller'=>$controller,'method'=>$action))->item();
            if(!$res){
                $this->request_error('对不起，访问的功能不存在');
            }
            if($res['status']==1){
                $this->request_error('对不起，该功能已禁止使用');
            }
            if(!in_array($res['mid'],$rights)){//判断当前菜单控制器方法的mid编码是否在该用户角色权限中
                $this->request_error('对不起，您没有权限'.$res['mid']);
            }
        }
    }
    private function request_error($msg){
        if(request()->isAjax()){//是否AJax请求
            //echo $msg;
            //dump($msg);
            //var_dump($msg);
            //file_put_contents('request.txt',$msg);
            exit(json_encode(array('code'=>1,'msg'=>$msg)));
        }
        exit($msg);
    }




    //获取后台用户数据
    public function get_user($iscache=true){
        $user = 'user';
        $user = Cache::get($user);
        if($user&&$iscache){
            return $user;
        }else{
            Cache::rm('user');
            $user = $this->db->table('user')->lists();
            Cache::set('user',$user,600);
            return $user;
        }
    }

    //获取房屋数据
    public function get_house($iscache=true){
        $house = 'house';
        $house = Cache::get($house);
        if($house&&$iscache){
            return $house;
        }else{
            Cache::rm('house');
            $house = $this->db->table('housing_resources')->lists();
            Cache::set('house',$house,600);
            return $house;
        }
    }

    //获取后台人员数据
    public function get_admin($iscache=true){
        $admin = 'admin';
        $admin = Cache::get($admin);
        if($admin&&$iscache){
            return $admin;
        }else{
            Cache::rm('admin');
            $admin = $this->db->table('admin_admins')->lists();
            Cache::set('admin',$admin,600);
            return $admin;
        }
    }

    //获取房屋数据
    public function get_room($iscache=true){
        $room = 'room';
        $room = Cache::get($room);
        if($room&&$iscache){
            return $room;
        }else{
            Cache::rm('room');
            $room = $this->db->table('room')->lists();
            Cache::set('room',$room,600);
            return $room;
        }
    }

    //获取商圈
    public function get_business_circle($iscache=true){
        $business_circle = 'business_circle';
        $business_circle = Cache::get($business_circle);
        if($business_circle&&$iscache){
            return $business_circle;
        }else{
            Cache::rm('business_circle');
            $business_circle = $this->db->table('business_circle')->lists();
            foreach ($business_circle as $k=>$v) {
                $arr[$v['bc_id']] = $v;
            }
            Cache::set('business_circle',$arr,600);
            return $arr;
        }
    }

    //获取小区
    public function get_residential_quarters($iscache=true){
        $residential_quarter = 'residential_quarter';
        $residential_quarter = Cache::get($residential_quarter);
        if($residential_quarter&&$iscache){
            return $residential_quarter;
        }else{
            Cache::rm('residential_quarter');
            $list = $this->db->table('residential_quarters')->lists();
            foreach ($list as $k=>$v) {
                $residential_quarter[$v['rq_id']] = $v;
            }
            Cache::set('residential_quarter',$residential_quarter,600);
            return $residential_quarter;
        }
    }

    //获取后台人员数据
    public function get_tenant($iscache=true){
        $tenant = 'tenant';
        $tenant = Cache::get($tenant);
        if($tenant&&$iscache){
            return $tenant;
        }else{
            Cache::rm('tenant');
            $tenant = $this->db->table('tenant')->lists();
            Cache::set('tenant',$tenant,600);
            return $tenant;
        }
    }

    //获取地址数据
    public function get_address($iscache=true){
        $address = 'address';
        $address = Cache::get($address);
        if($address&&$iscache){
            return $address;
        }else{
            Cache::rm('address');
            $address = $this->db->table('address')->lists();
            foreach ($address as $k => $v) {
                $arr[$v['a4']]=$v;
            }
            Cache::set('address',$arr,60000);
            return $arr;
        }
    }

}